package com.zr.sparesubclass.controller;

import com.zr.sparesubclass.bean.LegalPerson;
import com.zr.sparesubclass.model.AddPlantVo;
import com.zr.sparesubclass.model.PlantQueryVo;
import com.zr.sparesubclass.model.UpdateEnabledVo;
import com.zr.sparesubclass.service.PlantService;
import com.zr.util.AllRecords;
import com.zr.util.ResultVO;
import com.zr.util.ResultVOBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class PlantController {
    @Autowired
    private PlantService plantService;
    @GetMapping("/legalPerson/ofCurrentUser")
    @CrossOrigin
    public ResultVO<List<LegalPerson>> findLegalPersonByUserId(){
        //从session获取user.id
       return plantService.findLegalPersonByUserId(1);
    }
    @PostMapping("legalPlant/query")
    @CrossOrigin
    public ResultVO<AllRecords> legalPlantQuery(@RequestBody PlantQueryVo plantQueryVo){
        return  plantService.legalQueryVo(plantQueryVo);
    }
    @PostMapping("legalPlant/updatestatus")
    @CrossOrigin
    public ResultVO<UpdateEnabledVo> updatestatus(@RequestBody UpdateEnabledVo updateEnabledVo){
        return  plantService.updatestatus(updateEnabledVo);
    }
    @PostMapping("legalPlant/add")
    @CrossOrigin
    public ResultVO<AddPlantVo> add(@RequestBody @Valid AddPlantVo addPlantVo ,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return  plantService.add(addPlantVo);
    }

    @PostMapping("legalPlant/update")
    @CrossOrigin
    public ResultVO<AddPlantVo> update(@RequestBody @Valid AddPlantVo addPlantVo ,BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return  plantService.update(addPlantVo);
    }
}
